package cn.dinkevin.xui.util;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ListView;
import android.widget.ScrollView;

import cn.dinkevin.xui.R;

/**
 * View 相关工具类
 * Created by ChengPengFei on 2017/3/15 0015.</br>
 * Copyright ©2017 juziwl, All Rights Reserved.
 */

public final class ViewUtil {

    private ViewUtil(){}

    private static final int BLOCK_INTERVAL = 500;

    /**
     * 过滤快速重复点击
     * @param view
     * @return
     */
    public static boolean filterClickToClick(View view){
        return !blockClickToClick(view);
    }

    /**
     * 过滤快速重复点击
     * @param view
     * @param delayTime
     * @return
     */
    public static boolean filterClickToClick(View view,long delayTime){
        return !blockClickToClick(view,delayTime);
    }

    /**
     * 过滤快速重复点击控件</br>
     * 检测当前 View 是否可以点击，并更新可点击时间截止点</br>
     * 本接口实现的功能主要是过滤快速重复点击某控件
     * @param view
     * @return true -> 本次点击触发成功;false -> 本次触发失败
     */
    private static boolean blockClickToClick(View view){
        return blockClickToClick(view,BLOCK_INTERVAL);
    }

    /**
     * 过滤快速重复点击控件</br>
     * 检测当前 View 是否可以点击，并更新可点击时间截止点</br>
     * 本接口实现的功能主要是过滤快速重复点击某控件
     * @param view
     * @param delayTime 延时时间
     * @return true -> 本次点击过滤成功;false -> 本次点击不过滤
     */
    private static boolean blockClickToClick(View view, long delayTime){

        boolean active = true;
        Object tag = view.getTag(R.id.xui_container);
        if(tag != null && tag instanceof Long){
            long oldDelayTime = (Long)tag;
            long currentTime = System.currentTimeMillis();
            active = oldDelayTime + delayTime <= currentTime;
        }

        view.setTag(R.id.xui_container,delayTime < 1 ? System.currentTimeMillis() + BLOCK_INTERVAL : System.currentTimeMillis());
        return active;
    }

    /**
     * 将指定的控件从父 View 中移除自己
     * @param view
     */
    public static void removeSelfFromParent(View view){
        if(null != view){
            ViewParent parent = view.getParent();
            if(null != parent){
                ViewGroup group = (ViewGroup)parent;
                group.removeView(view);
            }
        }
    }

    /**
     * 是否已经滑动到顶部
     * @param view
     * @return
     */
    public static boolean isScrollToTop(View view) {
        if (view instanceof ListView) {
            ListView listView = (ListView) view;
            int first = listView.getFirstVisiblePosition();
            return first == 0;
        } else if (view instanceof RecyclerView) {
            RecyclerView recyclerView = (RecyclerView) view;
            return !recyclerView.canScrollVertically(-1);
        } else if (view instanceof ScrollView) {
            ScrollView scrollView = (ScrollView) view;
            return scrollView.getScrollY() == 0;
        }
        return true;
    }

    /**
     * 滑动到顶部
     * @param view
     */
    public static void scrollToTop (View view) {
        if (view instanceof ScrollView) {
            ScrollView scrollView = (ScrollView) view;
            scrollView.smoothScrollTo(0,0);
        } else if (view instanceof ListView) {
            ListView listView = (ListView) view;
            listView.setSelection(0);
        }
    }

    public static  <T> T findView(View view, Class<T> tClass) {
        if (view.getClass() == tClass) {
            return (T) view;
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i=0;i<viewGroup.getChildCount();i++) {
                View subView = viewGroup.getChildAt(i);
                T find = findView(subView,tClass);
                if (find!=null) {
                    return find;
                }
            }
        }
        return null;
    }
}
